home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / LibraryOfCongress / LibraryOfCongress.app / License.m < prev    next >
Encoding:
Text File  |  1992-08-07  |  8.2 KB  |  290 lines

  1. /*
  2.     Copyright (c) 1991, 1992 by the MIT Media Laboratory
  3.     
  4.     This software is distributed by Michael Hawley of the MIT Media Laboratory. 
  5.     We hope it will be useful to you.
  6.     
  7.     Permission to use, copy, or modify this software for educational 
  8.     and research purposes only and without fee is hereby granted 
  9.     provided this notice appears on all copies, and provided you send 
  10.     us your improvements.  Any other use of this software, in original 
  11.     or modified form, in whole or in part, requires specific permission 
  12.     from MIT.  This software shall not be used, rewritten, or adapted 
  13.     for use in a commercial product without first obtaining appropriate 
  14.     licenses from MIT.  MIT makes no representations about the suitability 
  15.     of this software for any purpose: it is provided "as is" without any 
  16.     warranty and any risk, damage, or liability incurred through your use 
  17.     of this software is yours alone.
  18.     
  19.     Michael Hawley
  20.     MIT Media Laboratory
  21.     20 Ames Street, 
  22.     Cambridge, MA 02139
  23.     mike@media-lab.mit.edu
  24. */
  25. /*
  26.  *
  27.  * Implement the License & info panels, receipt mechanism, and Suggestion box.
  28.  *
  29.  * The License declares the Media Laboratory copyright with an
  30.  * enjoinder to prevent people from re-selling the software,
  31.  * but a hearty encouragement to urge folks to modify and improve
  32.  * the code -- providing the improvements are sent back to us
  33.  * and the license remains intact.  The License also exempts the
  34.  * Media Lab from any blame due to any damage incurred by users.
  35.  *
  36.  * Acceptance of the License (by first-time users) sends a receipt
  37.  * (akin to a "read receipt" in Mail) back to the home base,
  38.  * to maintain a census of the population using the code.
  39.  *
  40.  * Note that the first time this was tried without mention of the
  41.  * outgoing receipt, many reacted badly to the implication that
  42.  * software might act "behind their back."  But an acknowledged
  43.  * receipt should not be problematic, and it is not unreasonable
  44.  * for the author to want to understand the user community.
  45.  *
  46.  * The other feedback channel here is the -suggestion: method,
  47.  * which composes an e-mail letter so user's comments, bugs, and
  48.  * improvements can be collected.
  49.  *
  50.  *    IMPORTANT: #define ReceiptsTo and CommentsTo, below, and prepare
  51.  *        the receiving mailboxes.
  52.  *    
  53.  *
  54.  * Michael Hawley
  55.  * mike@media-lab.mit.edu
  56.  */
  57.  
  58. #import <stdio.h>
  59. #import <stdlib.h>
  60. #import <strings.h>
  61. #import <appkit/Application.h>
  62. #import <appkit/Speaker.h>
  63. #import <appkit/Button.h>
  64. #import <appkit/Panel.h>
  65. #import <appkit/ScrollView.h>
  66. #import <appkit/Scroller.h>
  67. #import <appkit/Text.h>
  68. #import <soundkit/Sound.h>
  69. #import "Defaults.h"
  70. #import "License.h"
  71. #import "ColorBars.h"
  72. #import "ColorScroller.h"
  73.  
  74. @implementation License
  75.  
  76.         /* NULL or "" will disable receipt mechanism */
  77. #define ReceiptsTo    "receipts@tome.media.mit.edu"
  78. #define CommentsTo    "comments@tome.media.mit.edu"
  79.  
  80. #define AppName [NXApp appName]
  81. extern char *Version();
  82.  
  83. char *stripnl(char *s){ /* remove trailing space */
  84.     char *p;
  85.     for (p=s;*p;p++) if (*p == '\n' || *p == '\r') *p = '\0';
  86.     return s;
  87. }
  88.  
  89. char *
  90. execstr(s) char *s; { /* replace contents of 's' with piped output */
  91.     FILE *f = popen(s,"r");
  92.     char *p = s;
  93.     *s = '\0';
  94.     if (f){
  95.         while (fgets(p,256,f))
  96.             stripnl(p), p += strlen(p);
  97.         pclose(f);
  98.     }
  99.     return s;
  100. }
  101.  
  102. - windowWillClose:sender {
  103.     [infobars stop];
  104.     return self;
  105. }
  106.  
  107. static id _version;
  108.  
  109. char *
  110. Version(){
  111.     char *s = (char *)[_version stringValue];
  112.     return s? s : "1.0";
  113. }
  114.  
  115. int
  116. FirstUse(){ // true if this is the first time the app's been used
  117.     return (*FirstUsed)? 0 : 1;
  118. }
  119.  
  120. void
  121. mailReceipt(subj,body,to)
  122.     char *subj, *body, *to;
  123. /*
  124.  * Send a read/run receipt form to "ReceiptsTo" address,
  125.  * to indicate that the user has accepted the License.
  126.  * If "to" and "ReceiptsTo" are blank (or NULL), this is a no-op.
  127.  */
  128. {
  129.     char s[1024], u[256]="whoami", m[256]="hostname", t[4096], tmp[128];
  130.     FILE *f;
  131.     extern mktemp();
  132.  
  133.     if (!to || !*to)     to   = ReceiptsTo;
  134.     if (!to || !*to)     return;
  135.     if (!subj || !*subj) subj = (char *)AppName;
  136.     execstr(u); execstr(m);
  137.     if (!body || !*body){
  138.         body = t;
  139.         sprintf(body, "\nIf this receipt bounces back to you,\n\
  140. could you please correct the address if possible,\n\
  141. and aim it to: %s\n\
  142. to let us know you're registered.\n",ReceiptsTo);
  143.     }
  144.     strcpy(tmp,"/tmp/openerXXXXXX"); mktemp(tmp);
  145.     if (f = fopen(tmp,"w")){
  146.         fprintf(f,"%s",body);
  147.         fclose(f);
  148.         sprintf(s,"(/usr/ucb/Mail -s \"%s %s@%s\" %s < %s; rm %s) &",
  149.                 subj,u,m,to,tmp,tmp);
  150.         system(s);
  151.     }
  152. }
  153.  
  154. static int init = 0;
  155.  
  156. - setScrollview:t { // staple in a ColorScroller
  157.     id s;
  158.     NXRect r;
  159.     NXColor paleYellow = NXConvertRGBToColor(1.0, 238./256., 200./256.);
  160.     scrollview = t;
  161.     [[t vertScroller] getFrame:&r];
  162.     s = [[ColorScroller alloc] initFrame:&r];
  163.     [t setVertScroller:s];
  164.     t = [t docView];
  165.     [t setBackgroundColor:paleYellow];
  166.     // fix scrollbar thumb
  167.     [t setSel:0:0];
  168.     [t replaceSel:"\n"];
  169.     [t setEditable:NO];
  170.     return self;
  171. }
  172.  
  173. - (void) load {
  174.     if (!init){
  175.         init++, [NXApp loadNibSection:"License.nib" owner:self];
  176.     _version = version;
  177.     [infobars setBackgroundColor:NX_COLORLTGRAY];
  178.     [infobars setDrawstyle:CLEAR];
  179.     [infopanel setDelegate:self];
  180.         [panel setBackgroundGray:1.];
  181.         if (!FirstUse()){ // already registered, so rearrange the panel
  182.             init++;
  183.         [quit setIcon:"registered"];
  184.         [quit setBordered:NO];
  185.         [quit setEnabled:NO];
  186.         [quit sizeToFit];
  187.         [quit moveBy:-32.:-8.];
  188.         [accept setTitle:"Okay"];
  189.         }
  190.     }
  191. }
  192.  
  193. - suggestion:sender {
  194.     char subj[256], w[256] = "whoami";
  195.     char body[4096]="\
  196. Michael:\n\n\
  197. This is certainly a spiffy confabulation of public library,\n\
  198. private bookseller, the internet, and Federal Express!\n\
  199. As a token of my appreciation, I would love to assist your\n\
  200. spastic 8-man Media Crew team with a small contribution to help\n\
  201. cover the boathouse dues (tax-deductible contributions may\n\
  202. be sent to Crew Chief Bob Green, c/o MIT Media Lab, 20 Ames Street,\n\
  203. Cambridge, MA 02139).\n\n\
  204. Meanwhile, you will no doubt appreciate hearing about the following glitch:\n\n\
  205.    <insert irritating bug here>\n\n\
  206.              palimpsestuously,\n\
  207.              ";
  208. #define fcall(a)  [s performRemoteMethod:a]
  209. #define call(a,b) [s performRemoteMethod:a with:b length:strlen(b)+1]
  210.     id s = [NXApp appSpeaker];
  211.     extern NXPortFromName();
  212.  
  213.     NXPortFromName("Mail", NULL); // make sure app is launched
  214.     [s setSendPort:NXPortFromName("MailSendDemo", NULL)];
  215.  
  216.     [self load];
  217.     sprintf(subj,"Comments and suggestions for ``%s'' %s",AppName,Version());
  218.     sprintf(body+strlen(body),"%s\n",execstr(w));
  219.     call("setTo:",CommentsTo);
  220.     call("setSubject:",subj);
  221.     call("setBody:",body);
  222.     
  223.     return self;
  224. }
  225.  
  226. - show:sender {
  227.     [self load];
  228.     [colorbars setDrawstyle:CLEAR];
  229.     [panel makeKeyAndOrderFront:sender];
  230.     [colorbars setDrawstyle:SWAB];
  231.     [NXApp runModalFor:panel];
  232.     return self;
  233. }
  234.  
  235. - info:sender {
  236.     [self load];
  237.     [infobars setDrawstyle:CLEAR];
  238.     [infopanel makeKeyAndOrderFront:sender];
  239.     [infobars setDrawstyle:SWAB];
  240.     return self;
  241. }
  242.  
  243. - sendReceipt { // send a receipt to indicate the user has read the license
  244.     char *ctime(), s[1024];
  245.     long time(), t = time(0);
  246.  
  247.     sprintf(s,"%s-%s-registered",AppName,Version());
  248.     if (t < 793414430 /* 1995 */) mailReceipt(s,0,ReceiptsTo);
  249.     strcpy(FirstUsed,ctime(&t));
  250.     stripnl(FirstUsed);
  251.     writeDefaults();
  252.     return self;
  253. }
  254.  
  255. - accept:sender {
  256.     if (init < 2){ // accept the license: rearrange panel & send receipt
  257.     id s = [Sound findSoundFor:"Stamp"];
  258.         init++;
  259.     [panel disableFlushWindow];
  260.         [quit setIcon:"registered"];
  261.         [quit setBordered:NO];
  262.         [quit setEnabled:NO];
  263.         [quit sizeToFit];
  264.     [quit setTransparent:YES];
  265.         [quit moveBy:-32.:-8.];
  266.         [accept setTitle:"Okay"];
  267.     [panel display];
  268.     [[panel reenableFlushWindow] flushWindow];
  269.     NXPing();
  270.     [quit setTransparent:NO];
  271.     [s play:self];
  272.     [quit display];
  273.     NXPing();
  274.     [self sendReceipt];
  275.     }
  276.     [NXApp stopModal];
  277.     [colorbars stop];
  278.     [colorbars setDrawstyle:WINKOFF];
  279.     [panel orderOut:sender];
  280.     return self;
  281. }
  282.  
  283. - quit:sender {
  284.     [colorbars stop];
  285.     [colorbars setDrawstyle:WINKOFF];
  286.     exit(0);
  287. }
  288.  
  289. @end
  290.